package com.css.fxfzaqbz.modules.assess.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.DisasterAssEntity;
import com.css.fxfzaqbz.modules.assess.repository.DisasterRepository;
import com.css.fxfzaqbz.modules.assess.service.DisasterService;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-09-09 16:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DisasterServiceImpl implements DisasterService {

    @Resource
    private DisasterRepository disasterRepository;
    @Resource
    private SUserService sUserService;

    @Override
    public DisasterAssEntity saveDisasterAss(DisasterAssEntity disasterAssEntity) {
        if (disasterAssEntity.getSdaId()==null){
            disasterAssEntity.setSdaId(UUIDGenerator.getUUID());
        }
        disasterAssEntity.setDelFlag(YNEnum.N.toString());
        disasterAssEntity.setSdaAssDate(PlatformDateUtils.getCurrentTimestamp());
        disasterAssEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        disasterAssEntity.setCreateUser(userID);
        disasterAssEntity.setMark(YNEnum.N.toString());
        disasterAssEntity.setTypes("房屋地震灾害风险评估");
        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if (sUser!=null){
            disasterAssEntity.setSdaAssPeople(sUser.getUserName());
        }
        if (PlatformObjectUtils.isEmpty(disasterAssEntity.getDataMessage())){
            disasterAssEntity.setDataMessage("计算评估");
        }
        return disasterRepository.saveDisasterAss(disasterAssEntity);

    }

    @Override
    public List<DisasterAssEntity> getAllDisasterAss(String id) {
        return disasterRepository.getAllDisasterAss(id);
    }

    @Override
    public void deleteDisasterAss(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        disasterRepository.deleteDisasterAss(id,userID,currentTimestamp);
    }

    @Override
    public DisasterAssEntity getDisasterAssBySi(DisasterAssEntity disasterAssEntity) {
      return null;

    }

    @Override
    @Transactional
    public DisasterAssEntity updateDisasterAss(DisasterAssEntity disasterAssEntity, String sdaId) {
        disasterAssEntity.setSdaAssDate(PlatformDateUtils.getCurrentTimestamp());
        disasterAssEntity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        disasterAssEntity.setUpdateUser(userID);
/*

        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        disasterAssEntity.setSdaAssPeople(sUser.getUserName());
*/

        return disasterRepository.updateDisasterAss(disasterAssEntity,sdaId);
    }

    @Override
    public DisasterAssEntity getDisasterAssById(String id) {

        DisasterAssEntity disasterAssEntity = disasterRepository.getDisasterAssById(id);
        if (!PlatformObjectUtils.isEmpty(disasterAssEntity.getSdaAssFileDescribe())){
            List<String> list = dataString(disasterAssEntity.getSdaAssFileDescribe());
            disasterAssEntity.setDescribe(list);
        }       return disasterAssEntity;
    }

    @Override
    public DisasterAssEntity updateDisasterAssD(DisasterAssEntity disasterAssEntity) {
        return disasterRepository.updateDisasterAssD(disasterAssEntity);
    }

    @Override
    public void firstSubmitDisasterAss(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        DisasterAssEntity disasterAssById = disasterRepository.getDisasterAssById(id);
        List<DisasterAssEntity> disasterAssBySi = disasterRepository.getDisasterAssBySi(disasterAssById.getSdaAssSeiIntensity(), disasterAssById.getSdaRelation());

        if (!PlatformObjectUtils.isEmpty(disasterAssBySi)){

            if (!PlatformObjectUtils.isEmpty(disasterAssBySi.get(0).getSdaAssFileDescribe())){
                disasterAssById.setSdaAssFileDescribe(disasterAssBySi.get(0).getSdaAssFileDescribe());
                disasterRepository.firstSubmitDisasterAss(id,userID,currentTimestamp,disasterAssById.getSdaAssFileDescribe());
            }else {
                disasterRepository.firstSubmitDisasterAss2(id,userID,currentTimestamp);
            }
        }else {
            disasterRepository.firstSubmitDisasterAss2(id,userID,currentTimestamp);
        }
    }

    @Override
    public Boolean ifRepetition(DisasterAssEntity disasterAssEntity) {
        DisasterAssEntity  ifRepetitionDis=disasterRepository.ifRepetition(disasterAssEntity);
        if (ifRepetitionDis==null){
            return false;
        }else {
            return true;
        }

    }


    /*
    *
    * 查询最终成果
    * */
    @Override
    public List<DisasterAssEntity> getBigDisasterTwo(String id) {
        List<DisasterAssEntity> allDisasterAss = disasterRepository.getBigDisasterTwo(id);
        for (DisasterAssEntity disasterAss : allDisasterAss) {
            if (!PlatformObjectUtils.isEmpty(disasterAss.getSdaAssFileDescribe())){
                //处理字符串转集合 "@@@"
                List<String> disList=dataString(disasterAss.getSdaAssFileDescribe());
                disasterAss.setDescribe(disList);
            }
        }

        return allDisasterAss;
    }

    /*
     * 字符串转集合方法
     * */
    private List<String> dataString(String sdaAssFileDescribe) {
        String[] split = sdaAssFileDescribe.split("@@@");
        List<String> disList=new ArrayList<>();
        Collections.addAll(disList,split);
        return disList;
    }

    @Override
    public void addDescribe(List<String> ids, String describe) {
        disasterRepository.addDescribe(ids,describe);
    }

    @Override
    public void removeDisasterAss(String id) {
        DisasterAssEntity disasterAssById = disasterRepository.getDisasterAssById(id);
        disasterAssById.setMark(YNEnum.N.toString());
        disasterAssById.setSdaAssFileDescribe("");
        disasterAssById.setSubmiter("");
        disasterAssById.setSubmitPhone("");
        disasterRepository.removeDisasterAss(disasterAssById);
    }

    @Override
    public void secendSubmitDisasterAss(List<String> ids) {
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);

        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        disasterRepository.secendSubmitDisasterAss(ids,sUser,currentTimestamp);
    }

    @Override
    public List<DisasterAssEntity> getDisasterAssByIaId(String iaId) {
        return disasterRepository.getDisasterAssByIaaId(iaId);
    }


}
